package com.example.mvvmtest.view

import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.example.mvvmtest.R
import com.example.mvvmtest.databinding.ActivityLoginBinding
import com.example.mvvmtest.model.database.UserDatabase
import com.example.mvvmtest.Repository.UserRepository
import com.example.mvvmtest.viewModel.LoginActivityViewModel

class LoginActivity : AppCompatActivity() {
    private lateinit var binding: ActivityLoginBinding
    private lateinit var viewModel: LoginActivityViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        // 初始化数据绑定
        binding = DataBindingUtil.setContentView(this, R.layout.activity_login)

        // 初始化ViewModel
        val dao = UserDatabase.getDatabase(application).userDao()
        val repository = UserRepository(dao)
        viewModel = ViewModelProvider(this, object : ViewModelProvider.Factory {
            override fun <T : ViewModel> create(modelClass: Class<T>): T {
                if (modelClass.isAssignableFrom(LoginActivityViewModel::class.java)) {
                    return LoginActivityViewModel(repository) as T
                }
                throw IllegalArgumentException("Unknown ViewModel class")
            }
        })[LoginActivityViewModel::class.java]
        // 绑定ViewModel
        binding.viewModel = viewModel
        binding.lifecycleOwner = this

        // 设置输入框文本变化监听
        binding.userIdEdit.addTextChangedListener(object : android.text.TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: android.text.Editable?) {
                if (s != null) {
                    viewModel.onIdChanged(s.toString())
                }
            }
        })

        binding.userPasswordEdit.addTextChangedListener(object : android.text.TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: android.text.Editable?) {
                if (s != null) {
                    viewModel.onPasswordChanged(s.toString())
                }
            }
        })

        // 设置登录按钮点击事件
        binding.loginBtn.setOnClickListener {
            viewModel.login()
        }

        // 登录成功跳转MainActivity
        viewModel.loginSuccess.observe(this) { success ->
            if (success == true) {
                val intent = Intent(this, MainActivity::class.java)
                intent.putExtra("USER_ID", viewModel.id.value)
                startActivity(intent)
                finish()
            }
        }

        // 注册跳转
        binding.toRegister.setOnClickListener {
            startActivity(Intent(this, RegisterActivity::class.java))
        }
    }
}